package com.groupbuying.file.pojo;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;

/**
 * @Author wenguoqiang
 * oss服务类
 */
public class OSSServiceProvider {

    //oss id
    private String ossAccessKeyId;
    //oss 秘钥
    private String ossAccessKeySecret;
    // 节点
    private String endPoint;
    // 名称
    private String bucketName;
    // 客户端
    private OSSClient ossClient;

    public OSSServiceProvider(String ossAccessKeyId, String ossAccessKeySecret, String endPoint, String bucketName) {
        this.ossAccessKeyId = ossAccessKeyId;
        this.ossAccessKeySecret = ossAccessKeySecret;
        this.endPoint = endPoint;
        this.bucketName = bucketName;
        this.ossClient = new OSSClient(endPoint, ossAccessKeyId, ossAccessKeySecret);
    }


    public void putObject(String objectName,InputStream in){
        OSSClient ossClient = this.ossClient;
        ossClient.putObject(bucketName, objectName, in);
    }

    public void shutdown(){
        ossClient.shutdown();
    }

    public URL generatePresignedUrl(String fileName , Date expires){
        URL url = ossClient.generatePresignedUrl(bucketName, fileName, expires);
        return url;
    }

    public InputStream getOSSObjectStream (String fileName){
        OSSObject ossObject = ossClient.getObject(bucketName, fileName);
        return ossObject.getObjectContent();
    }
}
